<script setup lang="tsx">
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { useRouter, useRoute } from 'vue-router'
import { ElButton } from 'element-plus'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useI18n } from '@/hooks/web/useI18n'
import { useTable } from '@/hooks/web/useTable'
import { pageRankApi, adminPageRankApi } from '@/api/Activity'
import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()

const { t } = useI18n()
const { go, push } = useRouter()
const { query } = useRoute()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const api = userStore.userInfo.roles.includes('商家') ? adminPageRankApi : pageRankApi
    const res = await api({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      activeId: unref(query.id as any),
      ...unref(searchParams)
    })
    return {
      list: res.data.list || [],
      total: res.data.total || 0
    }
  }
})
const { total, loading, dataList, pageSize, currentPage } = tableState
const { getList } = tableMethods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: '分销排名',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'keyword',
    label: '经纪人',
    search: {
      componentProps: {
        placeholder: '请输入经纪人姓名/手机号'
      }
    },
    form: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          const row = data.row as any
          return (
            <>
              <div>{row.nickName}</div>
              <div>{row.mobile}</div>
            </>
          )
        }
      }
    }
  },
  {
    field: 'orderCount',
    label: '有效分销订单数',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'orderPrice',
    label: '分销订单金额',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'orderNum',
    label: '分销份数',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'estimateCommission',
    label: '预估佣金',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'commission',
    label: '结算佣金',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'time',
    label: '结算时间',
    search: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetimerange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    form: {
      hidden: true
    },
    table: {
      hidden: true
    }
  },
  {
    field: 'action',
    label: t('userDemo.action'),
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    search: {
      hidden: true
    },
    table: {
      width: 120,
      slots: {
        default: (data) => {
          const row = data.row
          return (
            <>
              <ElButton type="primary" link onClick={() => viewOrder(row)}>
                查看明细
              </ElButton>
            </>
          )
        }
      }
    }
  }
])

const viewOrder = (row) => {
  push(`/order/groupBuy?id=${query.id}&mobile=${row.mobile}`)
}

const { allSchemas } = useCrudSchemas(crudSchemas)

const searchParams = ref({})
const setSearchParams = (params: any) => {
  const { time, ...rest } = params
  let startSettleTime, endSettleTime
  if (time && time.length > 0) {
    startSettleTime = time[0]
    endSettleTime = time[1]
  }
  currentPage.value = 1
  searchParams.value = {
    startSettleTime,
    endSettleTime,
    ...rest
  }
  getList()
}
</script>

<template>
  <div class="flex w-100% h-100%">
    <ContentDetailWrap class="flex-[3] ml-20px" title="分销排名">
      <template #header>
        <ElButton @click="go(-1)">
          {{ t('common.back') }}
        </ElButton>
      </template>
      <Search
        :schema="allSchemas.searchSchema"
        @reset="setSearchParams"
        @search="setSearchParams"
      />
      <Table
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :columns="allSchemas.tableColumns"
        :data="dataList"
        :loading="loading"
        @register="tableRegister"
        :pagination="{
          total
        }"
      />
    </ContentDetailWrap>
  </div>
</template>
